#include "userinfo.h"
#include "ui_userinfo.h"
#include <QButtonGroup>
#include <QDoubleValidator>
#include <QMouseEvent>
#include <QDebug>
#include <QMessageBox>
#include <QStringList>
#include <QString>
#include <QDate>
#include <QColor>
#include <QPainter>
#include <QRect>
#include <QGraphicsDropShadowEffect>
#include <QFrame>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDir>
#include <QListWidget>

UserInfo::UserInfo(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::UserInfo)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);     //去掉主窗口边框
    this->setAttribute(Qt::WA_TranslucentBackground);  //设定窗口透明显示
    this->setWindowModality(Qt::WindowModal);  //设置窗口模态化

    //身高
    QRegExp regx("[1-9][0-9.]+$");  // lineEdit 的第一个数是1-9的，第二个数和之后的是0-9的
    QValidator *validator = new QRegExpValidator(regx, ui->lineEdit_height);
    ui->lineEdit_height->setValidator(validator);
    QDoubleValidator *pDfValidator = new QDoubleValidator(1, 300, 1, ui->lineEdit_height);  //范围1~300，保留一位小数
    pDfValidator->setNotation(QDoubleValidator::StandardNotation);
    ui->lineEdit_height->setValidator(pDfValidator);

    //体重
    QRegExp regx1("[1-9][0-9.]+$");  // lineEdit 的第一个数是1-9的，第二个数和之后的是0-9的
    QValidator *validator1 = new QRegExpValidator(regx, ui->lineEdit_weight);
    ui->lineEdit_weight->setValidator(validator1);
    QDoubleValidator *pDfValidator1 = new QDoubleValidator(1, 500, 1, ui->lineEdit_weight);  //范围1~500，保留一位小数
    pDfValidator->setNotation(QDoubleValidator::StandardNotation);
    ui->lineEdit_weight->setValidator(pDfValidator1);

    //鞋码
    QRegExp regx2("[1-9][0-9.]+$");  // lineEdit 的第一个数是1-9的，第二个数和之后的是0-9的
    QValidator *validator2 = new QRegExpValidator(regx, ui->lineEdit_shoeSize);
    ui->lineEdit_shoeSize->setValidator(validator2);
    QDoubleValidator *pDfValidator2 = new QDoubleValidator(1, 100, 1, ui->lineEdit_shoeSize);  //范围1~100，保留一位小数
    pDfValidator->setNotation(QDoubleValidator::StandardNotation);
    ui->lineEdit_shoeSize->setValidator(pDfValidator2);

    ui->lineEdit_height->setMaxLength(10);
    ui->lineEdit_weight->setMaxLength(10);
    ui->lineEdit_shoeSize->setMaxLength(10);


    //分组 按钮
    QButtonGroup *block1 = new QButtonGroup(this);  //性别
    QButtonGroup *block2 = new QButtonGroup(this);  //模式
    block1->addButton(ui->radioButton_male,0);
    block1->addButton(ui->radioButton_female,1);

    block2->addButton(ui->radioButton_3,0);
    block2->addButton(ui->radioButton_4,1);
    block2->addButton(ui->radioButton_5,2);
    block2->addButton(ui->radioButton_6,3);
    block2->addButton(ui->radioButton_7,4);
    block2->addButton(ui->radioButton_8,5);
    block2->addButton(ui->radioButton_9,6);
    block2->addButton(ui->radioButton_10,7);
    block2->addButton(ui->radioButton_11,8);
    block2->addButton(ui->radioButton_12,9);
    block2->addButton(ui->radioButton_13,10);
    block2->addButton(ui->radioButton_14,11);





}

UserInfo::~UserInfo()
{
    delete ui;
}

void UserInfo::mousePressEvent(QMouseEvent *event)
{
     this->windowPos = this->pos();                // 获得部件当前位置
     this->mousePos = event->globalPos();     // 获得鼠标位置
     this->dPos = mousePos - windowPos;       // 移动后部件所在的位置
}

void UserInfo::mouseMoveEvent(QMouseEvent *event)
{
     this->move(event->globalPos() - this->dPos);
}

void UserInfo::initUserInfo()
{
    qDebug()<<"填写所有用户信息";

}

QJsonObject userObj ;
//用户录入界面信息保存
bool UserInfo::saveUserInfo()
{
    user.username = ui->lineEdit_name->text();
    user.height = ui->lineEdit_height->text();
    user.weight =ui->lineEdit_weight->text();
    user.birth = ui->dateEdit->text();
    user.phone = ui->lineEdit_phone->text();
    user.shoeSize = ui->lineEdit_shoeSize->text();
    user.option = ui->lineEdit_option->text();
    user.applicant = ui->lineEdit_applicant->text();
    user.checker = ui->lineEdit_checker->text();

    if(ui->radioButton_male->isChecked() == true) {
        user.sex = "男";
    }
    else if(ui->radioButton_female->isChecked() == true){
        user.sex = "女";
    }

    if(ui->radioButton_3->isChecked() == true) {
        user.mode = "瞬时模式";
    }
    else if(ui->radioButton_4->isChecked() == true){
        user.mode = "双足站立睁眼模式";
    }
    else if(ui->radioButton_5->isChecked() == true){
        user.mode = "双足站立闭眼模式";
    }
    else if(ui->radioButton_6->isChecked() == true){
        user.mode = "单足站立睁眼模式(左)";
    }
    else if(ui->radioButton_7->isChecked() == true){
        user.mode = "单足站立睁眼模式(右)";
    }
    else if(ui->radioButton_8->isChecked() == true){
        user.mode = "单足站立闭眼模式(右)";
    }
    else if(ui->radioButton_9->isChecked() == true){
        user.mode = "单足站立睁眼模式(左)";
    }
    else if(ui->radioButton_10->isChecked() == true){
        user.mode = "跑步机模式";
    }
    else if(ui->radioButton_11->isChecked() == true){
        user.mode = "上坡模式";
    }
    else if(ui->radioButton_12->isChecked() == true){
        user.mode = "上楼梯模式";
    }
    else if(ui->radioButton_13->isChecked() == true){
        user.mode = "下楼梯模式";
    }
    else if(ui->radioButton_14->isChecked() == true){
        user.mode = "下坡模式";
    }

    if(user.username.isEmpty() || user.height.isEmpty() || user.weight.isEmpty() || user.sex.isEmpty() || user.phone.isEmpty() ||user.mode.isEmpty() ||user.shoeSize.isEmpty()  || user.applicant.isEmpty() || user.checker.isEmpty())
    {
        qDebug("请填写完整信息！\n");
        QMessageBox::about(NULL,"提示","请填写完整信息！\n");
        return false;
    }


    QString setting_path = "userInfoSetting.txt";
    QFile setting(setting_path);
    if(!setting.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
    {
        qDebug("保存用户信息失败！\n");
        QMessageBox::about(NULL,"提示","保存用户信息失败！\n");
        return false;
    }

    QTextStream txtInput(&setting);
    /**
//    QStringList user;
//    user<< "姓名：" + username;
//    txtInput << user[0] <<endl;
//    user << "身高：" + height;
//    txtInput << user[1] <<endl;
//    user<< "体重：" + weight;
//    txtInput << user[2] <<endl;
//    user<< "性别：" + sex;
//    txtInput << user[3] <<endl;
//    user<< "出生日期：" + birth;
//    txtInput << user[4] <<endl;
//    user<<"联系电话：" + phone;
//    txtInput << user[5] <<endl;
//    user<<"鞋码：" + shoeSize;
//    txtInput << user[6] <<endl;
//    user<< "模式：" + mode;
//    txtInput << user[7] <<endl;
//    user<< "主诉：" + option;
//    txtInput << user[8] <<endl;
//    user<<"申请人：" + applicant;
//    txtInput <<  user[9] <<endl;
//    user<< "检查人：" + checker;
//    txtInput << user[10] <<endl;
*/


    userObj.insert("姓名",user.username);
    userObj.insert("身高",user.height);
    userObj.insert("体重",user.weight);
    userObj.insert("性别",user.sex);
    userObj.insert("出生日期",user.birth);
    userObj.insert("联系电话",user.phone);
    userObj.insert("鞋码",user.shoeSize);
    userObj.insert("模式",user.mode);
    userObj.insert("主诉",user.option);
    userObj.insert("申请人",user.applicant);
    userObj.insert("检查人",user.checker);
    qDebug()<<"病人信息为："<<userObj;

    /*获取文件夹的名字，将QString类型转换为char*类型 */
    QString filename1 = user.username;//文件夹名字
    std::string str111 = filename1.toStdString();//转换为string类型，方便转换为char*类型
    const char* c_str2 = str111.c_str();//将子文件夹名字转换为char*类型，存放在c_str2中

    /*在根目录 srcDirPath 下创建子文件夹*/
    QDir dir(QDir::currentPath());
    if (!dir.exists(c_str2))  //创建二级子文件夹
        {
            //填入文件夹名字
            dir.mkdir(QString::fromUtf8(c_str2));
            qDebug()<<"用户文件夹生成";
        }

    QString filePath = QDir::currentPath()  + "/" + user.username;
    qDebug()<<"filePath:"<<filePath;


    // 将json对象里的数据转换为字符串
    QJsonDocument doc;
    // 将object设置为本文档的主要对象
    doc.setObject(userObj);

    // Json字符串保存到json文件里
    QString fileName = filePath + "/userInfo.json";
    QFile file(fileName);
    qDebug()<<"最后生成的fileName:"<<fileName;
     if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
          qDebug() << "can't open error!";
          return false;
     }
     QTextStream stream(&file);
     stream.setCodec("UTF-8");		// 设置写入编码是UTF8
     // 写入文件
     stream << doc.toJson();
     file.close();

    setting.close();
    return true;
}

void UserInfo::on_btn_cannel_clicked()
{
    this->close();
}

void UserInfo::on_btn_submit_clicked()
{
    if(saveUserInfo()) {
        QMessageBox::about(NULL,"成功","用户信息录入成功!");
        emit sendUserInfo(userObj);
        this->close();

    }
}
